package dao;

import domain.Fruit;
import domain.Manager;

import java.io.*;
import java.util.ArrayList;
import java.util.TreeSet;

public class FruitDao {
    private static ArrayList<Manager> manList = new ArrayList<>();
    private static ArrayList<Fruit> fruits = new ArrayList<>();
    private static TreeSet<Fruit> allFruits = new TreeSet<>((o1, o2) -> {
        int result = o2.getInventory() - o1.getInventory();
        result = result == 0 ? o1.getNum().compareTo(o2.getNum()) : result;
        return result;
    });
    private static TreeSet<Fruit> newFruits = new TreeSet<>((o1, o2) -> {
        int result = o2.getPrice() - o1.getPrice();
        result = result == 0 ? o1.getNum().compareTo(o2.getNum()) : result;
        return result;
    });

    static {
        read();
        read1();
    }

    public boolean delete(String num) {
        int index = getIndex(num);
        if (index != -1) {
            fruits.remove(index);
            reSave();
        }
        return true;
    }

    public boolean add(Fruit f) {
        fruits.add(f);
        //存档
        reSave();
//        reSave02();
        return true;
    }
    public Fruit[] findAllFruit() {
        //1. 定义一个数组,数组的长度和集合容器的长度一致
        Fruit[] fruArray = new Fruit[fruits.size()];

        fruits.toArray(fruArray);

        return fruArray;
    }
    public ArrayList<Manager> login() {
        read1();
        return manList;
    }

    public static void read1() {
        try {
            BufferedReader reader = new BufferedReader(new FileReader("server\\user.txt"));
            String line;
            while ((line = reader.readLine()) != null) {
                String[] str = line.split(",");
                Manager man = new Manager(str[0], str[1]);
                manList.add(man);
            }
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void inventory(String fnum, int num, int[] arr) {
        for (int i = 0; i < fruits.size(); i++) {
            Fruit f = fruits.get(i);
            if (f.getNum().equals(fnum)) {
                if (f.getInventory() >= num) {
                    arr[i] = num;
                    int j = f.getInventory() - num;
                    f.setInventory(j);
                }
            }
        }
        reSave();
    }

    public TreeSet<Fruit> sortByPrice() {
        return newFruits;
    }

    public TreeSet<Fruit> sortByInventory() {
        return allFruits;
    }

    public static void read() {
        try {
            BufferedReader reader = new BufferedReader(new FileReader("server\\fruit.txt"));
            String line;
            while ((line = reader.readLine()) != null) {
                String[] str = line.split(",");
                Fruit f = new Fruit(str[0], str[1], Integer.parseInt(str[2]), Integer.parseInt(str[3]), str[4]);
                fruits.add(f);
            }
            allFruits.addAll(fruits);
            reader.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public ArrayList<Fruit> findAllFruits() {
        return fruits;
    }

    public void reSave() {
        try {
            BufferedWriter bw = new BufferedWriter(new FileWriter("server\\fruit.txt"));
            for (Fruit f : fruits) {
                bw.write(f.getNum() + "," + f.getName() + "," + f.getPrice() + "," + f.getInventory() + "," + f.getUnit());
                bw.newLine();
            }
            bw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /*public void add(String num, String name, int price, int inventory, String unit) {
        Fruit f = new Fruit(num, name, price, inventory, unit);
        fruits.add(f);
        reSave();
    }*/

    /*public void delete(String num) {
        int index = getIndex(num);
        if (index != -1) {
            fruits.remove(index);
            reSave();
        }
    }*/

    public int getIndex(String num) {
        int index = -1;
        for (int i = 0; i < fruits.size(); i++) {
            Fruit f = fruits.get(i);
            if (f.getNum().equals(num)) {
                index = i;
            }
        }
        return index;
    }

    public boolean update(String num, Fruit newFruit) {
        int index = getIndex(num);
        fruits.set(index, newFruit);
        reSave();
        return true;
    }
}
